package com.example.electronicwoodfish;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    TextView title;
    int num = 0;
    ImageView muyu,gongde;
    ViewModel mViewModel;
    SoundPool sp;//声明SoundPool的引用
    HashMap<Integer, Integer> hm;//声明HashMap来存放声音文件
    int currStaeamId;//当前正播放的streamId

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        title = findViewById(R.id.textView);
        title.setText("功德："+num);
        muyu = findViewById(R.id.muyu);
        gongde = findViewById(R.id.gongde);

        initSoundPool();//初始化声音池的方法


        mViewModel = new ViewModelProvider(this).get(ViewModel.class);
        muyu.setScaleX(mViewModel.scaleFactor);
        muyu.setScaleY(mViewModel.scaleFactor);

        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(muyu,"scaleX",0,0);   //scale有两个  X方向和Y方向
        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(muyu,"scaleY",0,0);
        objectAnimatorX.setDuration(500);
        objectAnimatorY.setDuration(500);
        muyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playSound(1, 0);//播放1号声音资源，且播放一次
                showFadeAnimation();
                num += 1;
                title.setText("功德："+num);

                if (!objectAnimatorX.isRunning()){
                    float ScaleX = muyu.getScaleX();
                    float ScaleY = muyu.getScaleY();
                    objectAnimatorX.setFloatValues(ScaleX + 0.1f);  //放大0.1
                    objectAnimatorY.setFloatValues(ScaleY + 0.1f);
                    mViewModel.scaleFactor += 0.1;
                    objectAnimatorX.start();
                    objectAnimatorY.start();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(200); // 休眠0.5秒
                            }catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            objectAnimatorX.setFloatValues(ScaleX);
                            objectAnimatorY.setFloatValues(ScaleY);
                        }
                    }).start();
                }
            }
        });
    }

    private void showFadeAnimation() {
        //淡入淡出动画需要先定义一个图形资源数组，用于变化图片
        Drawable[] drawableArray = {
                getResources().getDrawable(R.mipmap.gongde),
                getResources().getDrawable(R.mipmap.bc)
        };
        //创建一个用于淡入淡出动画的过渡图形
        TransitionDrawable td_fade = new TransitionDrawable(drawableArray);
        //设置图像视图的图像为过渡图形
        gongde.setImageDrawable(td_fade);
        //开始过渡图形的变换过程，其中变换时长为3秒
        td_fade.startTransition(400);
    }

    private void playSound(int sound, int loop) {//获取AudioManager引用
        AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        //获取当前音量
        float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        //获取系统最大音量
        float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        //计算得到播放音量
        float volume = streamVolumeCurrent / streamVolumeMax;
        //调用SoundPool的play方法来播放声音文件
        currStaeamId = sp.play(hm.get(sound), volume, volume, 1, loop, 1.0f);
    }

    private void initSoundPool() {
        sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);//创建SoundPool对象
        hm = new HashMap<Integer, Integer>();//创建HashMap对象
        //加载声音文件，并且设置为1号声音放入hm中
        hm.put(1, sp.load(this, R.raw.music, 1));
    }
}